package com.runen.infinitemovement.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.MyFocusAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.base.BaseResponseEntity;
import com.runen.infinitemovement.model.entity.MyFocusEntity;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.infinitemovement.widget.popupwind.CommonDialog;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyFocusActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.edt_input_search)
    EditText edtInputSearch;
    @BindView(R.id.rv_focus)
    RecyclerView rvFocus;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    private MyFocusAdapter myFocusAdapter;
    private String edtString = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_focus;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("我的关注");
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        rvFocus.setLayoutManager(layoutManager);
        myFocusAdapter = new MyFocusAdapter(this);
        myFocusAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvFocus.setAdapter(myFocusAdapter);
        edtInputSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND) {//修改回车键功能
                    //代码
                    edtString = v.getText().toString().trim();
                    initData();
                }
                return false;
            }
        });
        initData();
        myFocusAdapter.setOnItemFocusClickListener(new MyFocusAdapter.OnItemFocusClickListener() {
            @Override
            public void addFocus(MyFocusEntity.DataBean item, int position) {

            }

            @Override
            public void cancelFocus(MyFocusEntity.DataBean item, int position) {
                CommonDialog.getInstance(MyFocusActivity.this, "", "不再关注此人？", "取消", "不再关注", 0).setOnClickListener(new CommonDialog.OnClickListener() {
                    @Override
                    public void onSendListener() {
                    }

                    @Override
                    public void onDismiss() {
                        onCancelFocus(item, position);
                    }
                });

            }
        });

        initLoadMore();
    }

    private void initLoadMore() {
        myFocusAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreRequested();
            }
        });
        myFocusAdapter.getLoadMoreModule().setAutoLoadMore(true);
        myFocusAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }

    /**
     * 取消关注
     *
     * @param item
     */
    private void onCancelFocus(MyFocusEntity.DataBean item, int position) {
        showDialog();
        JSONObject object = new JSONObject();
        try {
            object.put("unConcernedUserId", item.concernedUserId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new UserAPI().cancelFocus(object.toString(), new BaseResultCallback<BaseResponseEntity<?>>() {
            @Override
            public void onResponse(BaseResponseEntity<?> response) {

            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
                ToastUtils.showShort("取消关注成功");
                myFocusAdapter.remove(position);
                myFocusAdapter.notifyDataSetChanged();
            }
        });

    }

    private int page = 2;

    @OnClick(R.id.top_btn_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }

    private void initData() {
        srl.setRefreshing(true);
        String string = "?limit=20&page=1&userNameOrMoblie=%s";
        new UserAPI().getMyFocus(String.format(string, edtString), new BaseResultCallback<MyFocusEntity>() {
            @Override
            public void onResponse(MyFocusEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        myFocusAdapter.setNewData(response.data);
                        if (myFocusAdapter.getData().size() >= response.count) {
                            myFocusAdapter.getLoadMoreModule().loadMoreEnd(true);
                            myFocusAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        }

                    }
                }
            }
        });
    }

    public void loadMoreRequested() {
        String string = "?limit=20&page=%s&userNameOrMoblie=%s";
        new UserAPI().getMyFocus(String.format(string, page, edtString), new BaseResultCallback<MyFocusEntity>() {
            @Override
            public void onResponse(MyFocusEntity response) {
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        myFocusAdapter.addData(response.data);
                        if (myFocusAdapter.getData().size() >= response.count) {
                            myFocusAdapter.getLoadMoreModule().loadMoreEnd(true);
                            myFocusAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        } else {
                            page++;
                            myFocusAdapter.getLoadMoreModule().loadMoreComplete();
                            myFocusAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        }
                    }
                }
            }
        });
    }
}
